#!/usr/bin/env node


/*method | URL                           | function                   |
| -----  | ----------------------------- | -------------------------- |
| GET    | /api/authors/?page=1&limit=10 | 得到漫画书作者列表         |
| GET    | /api/authors/:authorName/     | 按作者名称查询             |
| POST   | /api/authors/                 | 添加新的漫画书作者         |
| PUT    | /api/authors/:id              | 修改某 ID 的漫画书作者信息 |
| DELETE | /api/authors/:id              | 删除某 ID 的漫画书作者信息 |*/

const Koa = require('koa');

const app = new Koa(),
      log = console.log;

app.use(async (ctx, next) => {

  // curl http://localhost:3000/authors/\?page\=22\&limit\=10
  if(ctx.method === 'GET' && ctx.path === '/authors/') {
    log('得到漫画书作者列表');
    ctx.body = '查询成功';
  }


  //curl http://localhost:3000/authors/wangding/\?page\=22\&limit\=10
  if(ctx.method === 'GET' && /\/authors\/\w/.test(ctx.path)) {
    log('按作者名查询');
    ctx.body = '查询成功';
  }

  //curl -X POST http://localhost:3000/authors/
  if(ctx.method === 'POST' && ctx.path === '/authors/') {
    log('添加新的漫画书作者');
    ctx.body = '添加成功';
  }

  //curl -X PUT -d 'abc' http://localhost:3000/authors/22
  if(ctx.method === 'PUT' && /\/authors\/(\d)+$/.test(ctx.path)) {
    log('修改某 ID 的漫画书作者名称');
    ctx.body = '修改成功';
  }


  //curl -X DELETE http://localhost:3000/authors/22  
  if(ctx.method === 'DELETE' && /\/authors\/(\d)+$/.test(ctx.path)) {
    log('删除某 ID 的漫画书作者');
    ctx.body = '删除成功';
  }
});

app.listen(3000);
